﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace WindowsFormsApplication1
{
    class DiagnoseDB
    {
        static string PATH = ".//..//..//diagnose.dbf";

        static public List<Diagnose> GetDiagnoses()
        {
            ArrayList lines = new ArrayList(File.ReadAllLines(PATH));
            List<Diagnose> res = new List<Diagnose>();

            foreach (var elem in lines)
            {
                Diagnose diag = Diagnose.ParseFromDb((string)elem);
                res.Add(diag);
            }

            return res;
        }

        public static int GetNewId(List<Diagnose> lines)
        {
            int newId;
            if (lines.Count > 0)
            {
                Diagnose lastDiag = (Diagnose)lines[lines.Count - 1];
                newId = lastDiag.ID.Value + 1;
            }
            else
            {
                newId = 1;
            }
            return newId;
        }

        public static void SaveDiagnoseToDB(Diagnose diagNew)
        {
            List<Diagnose> lines = GetDiagnoses();
            int newId = GetNewId(lines);
            diagNew.ID = newId;
            SaveDiagnoseToDB(diagNew, lines);
        }

        public static void SaveDiagnoseToDB(Diagnose diagNew, List<Diagnose> lines)
        {
            File.WriteAllText(PATH, "");
            Diagnose curDiag;
            bool diagFound = false;
            int i = 0;

            while (i < lines.Count)
            {
                curDiag = (Diagnose)lines[i];
                if (curDiag.ID == diagNew.ID)
                {
                    File.AppendAllText(PATH, diagNew.ToDbFormat() + Environment.NewLine);
                    diagFound = true;
                    i++;
                }
                else
                {
                    File.AppendAllText(PATH, curDiag.ToDbFormat() + Environment.NewLine);
                    i++;
                }
            }
            if (diagFound == false)
            {
                File.AppendAllText(PATH, diagNew.ToDbFormat() + Environment.NewLine);
            }

        }


        public static void DeleteDiagnose(Diagnose diagToDel)
        {
            List<Diagnose> res = GetDiagnoses();
            File.WriteAllText(PATH, "");
            // bool ifDeleted = false;
            foreach (var elem in res)
            {
                Diagnose diag = (Diagnose)elem;
                if (diag.ID != diagToDel.ID)
                {
                    File.AppendAllText(PATH, diag.ToDbFormat() + Environment.NewLine);
                }
                /* else
                 {
                     ifDeleted = true;
                 }*/
            }
            //  return ifDeleted;
        }

        public static void DeleteDiagnose(int index)
        {
            List<Diagnose> res = GetDiagnoses();
            File.WriteAllText(PATH, "");
            for (int i = 0; i < res.Count; i++)
            {
                Diagnose diag = (Diagnose)res[i];
                if (i != index)
                {
                    File.AppendAllText(PATH, diag.ToDbFormat() + Environment.NewLine);
                }
            }

        }

        public static void EditedDiagnoseToDB(int index, Diagnose editedDiag)
        {
            List<Diagnose> res = GetDiagnoses();
            File.WriteAllText(PATH, "");
            for (int i = 0; i < res.Count; i++)
            {
                Diagnose diag = (Diagnose)res[i];
                if (i != index)
                {
                    File.AppendAllText(PATH, diag.ToDbFormat() + Environment.NewLine);
                }
                else
                {
                    File.AppendAllText(PATH, editedDiag.ToDbFormat() + Environment.NewLine);
                }
            }

        }
        static public bool TryParseID(string id)
        {
            bool valid = false;
            int number;
            if (Int32.TryParse(id, out number) == true)
            {
                valid = true;
            }
            return valid;
        }

        static public List<int> GetDiagnoseID(string diagStr)
        {
            string[] diags = diagStr.Split(',');
            List<int> diagID = new List<int>();
            List<Diagnose> dList = GetDiagnoses();
            foreach (string el in diags)
            {
                for (int i = 0; i < dList.Count; i++)
                {
                    if (dList[i].name == el)
                    {
                        diagID.Add(dList[i].ID.Value);
                    }
                }
            }
            return diagID;
        }

        static public List<int> GetDiagnoseID(List<string> diagStr)
        {
            List<int> diagID = new List<int>();
            List<Diagnose> diagList = GetDiagnoses();
            foreach (string el in diagStr)
            {
                for (int i = 0; i < diagList.Count; i++)
                {
                    if (diagList[i].name == el)
                    {
                        diagID.Add(diagList[i].ID.Value);
                    }
                }
            }
            return diagID;
        }

        static public List<string> DiagNames(List<int> diags)
        {
            List<Diagnose> diagList = DiagnoseDB.GetDiagnoses();
            List<string> dNames = new List<string>();
            foreach (int el in diags)
            {
                foreach (Diagnose elem in diagList)
                {
                    if (el == elem.ID)
                    {
                        dNames.Add(elem.name);
                    }
                }
            }
            return dNames;
        }

        static public List<string> GetDiagnoseNames(Patient p)
        {
            List<Diagnose> diagList = DiagnoseDB.GetDiagnoses();
            List<string> dNames = new List<string>();

           if (p.patDiagnose == null)
            {
             // dNames.Add("");
               return dNames;
            }

            foreach (int el in p.patDiagnose)
            {
                foreach (Diagnose d in diagList)
                {
                    if (d.ID == el)
                    {
                        dNames.Add(d.name);
                    }
                }
            }
            return dNames;
        }

    }
}
